home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODF-Interest Archive / July 96 / Bug in FW_CStrings < prev    next >
Encoding:
Internet Message Format  |  1996-07-24  |  2.4 KB  |  [TEXT/ttxt]

  1. Subject:     Bug in FW_CStrings
  2. Sent:        7/23/96 5:31 PM
  3. Received:    7/23/96 5:41 PM
  4. From:        Kirk Swenson, kswenson@keypress.com
  5. Reply-To:    ODF Interest, ODF-Interest@CILabs.ORG
  6. To:          OpenDoc Development Framework Discussion List, ODF-Interest@CILabs.
  7.  
  8. There appears to be a bug in ODF's reference-counted string handling when
  9. assigning from bounded strings (e.g. FW_CString32) to unbounded ones (e.g.
  10. FW_CString).  It took a while to isolate, and I haven't pinpointed it
  11. exactly, but the following code demonstrates it:
  12.  
  13. void    TestStrings();
  14. void    SetString( FW_CString& dst, char c);
  15.  
  16. void    TestStrings()
  17. {
  18.         FW_CString              dynamicStr1;
  19.         FW_CString              dynamicStr2;
  20.         Str31                   tStr1, tStr2;
  21.  
  22.         // After SetString, dynamicStr1 should be all 1's.
  23.         // Instead it is a partially corrupted list of 1's.
  24.         SetString( dynamicStr1, '1');
  25.         dynamicStr1.ExportPascal( tStr1);
  26.  
  27.         // After this SetString call, both dynamicStr1 and dynamicStr2
  28.         // contain corrupted 2's, even though nothing should have
  29.         // happened to dynamicStr1.
  30.         SetString( dynamicStr2, '2');
  31.         dynamicStr1.ExportPascal( tStr1);
  32.         dynamicStr2.ExportPascal( tStr2);
  33. }
  34.  
  35. // Sets the destination string via a temporary bounded string
  36. void    SetString( FW_CString& dst, char c)
  37. {
  38.         FW_CString32    staticStr;
  39.  
  40.         // Initialize the temporary string
  41.         for( int i=0; i<30; ++i)
  42.                 staticStr += c;
  43.  
  44.         // The destination string is now equivalent to the temporary string.
  45.         // In ODF's reference-counting scheme, dst and staticStr refer to
  46.         // the same internal representation.
  47.         dst = staticStr;
  48.  
  49.         // Resetting staticStr should break the connection between the
  50.         // destination string and the temporary string such that the
  51.         // destination string is not affected.
  52.         staticStr = "";
  53. }
  54.  
  55. The bug does not manifest itself if the temporary string is not reset.
  56. Resetting staticStr appears to decrement the reference count of the static
  57. representation, but doesn't re-allocate it dynamically, which means that
  58. FW_CString32's destructor has a chance to corrupt part of the buffer.
  59. Unfortunately, I can't point to particular lines of code that are at fault,
  60. but this should get you to the vicinity.  I presume this is not the
  61. expected behavior?
  62.  
  63. Kirk Swenson
  64. Senior Software Engineer
  65. Key Curriculum Press
  66. kswenson@keypress.com
  67.  
  68.  
  69.